package com.example.wechat_b.fragment;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.wechat_b.adapter.ChatListViewAdapter;
import com.example.wechat_b.R;
import com.example.wechat_b.bean.ChatBean;
import com.example.wechat_b.utils.SqliteDatabaseHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liaoqg
 * @brief description
 * @date 2021-11-01
 */
public class ChatFragment extends Fragment {

    private ListView chatListView;
    private ChatListViewAdapter chatListViewAdapter;
    private SqliteDatabaseHelper helper;
    private SQLiteDatabase db;
    private List<ChatBean> beans;
    private ChatBean bean;
    private ImageView plus,find;
    private TextView time;

    public ChatFragment() {
        Log.e("cus", "cus");
//        if (getActivity()!=null){
//            helper = new SqliteDatabaseHelper(getActivity(),"withX",null,2);
//            db = helper.getReadableDatabase();
//            beans = new ArrayList<>();
//        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Create", "create");
        if (helper == null) {
            helper = new SqliteDatabaseHelper(getActivity(), "withX", null, 2);
        }
        if (db == null) {
            db = helper.getReadableDatabase();
//            db.execSQL("create table luofukun(id INTEGER PRIMARY KEY AUTOINCREMENT,type int,time String,token String)");
        }
        if (beans == null) {
            beans = new ArrayList<>();
        }
        if (bean == null){
            bean = new ChatBean();
            Cursor cursor = db.query("jipeng", null, null, null, null, null, "id desc", "1");
            if (cursor.getCount() != 0) {
                cursor.moveToLast();
                bean.setId(1);
                bean.setImage(R.mipmap.xjp);
                bean.setTitle("辛集朋");
                bean.setCurrent(cursor.getString(cursor.getColumnIndex("token")));
                bean.setTitle(cursor.getString(cursor.getColumnIndex("time")));
                beans.add(bean);
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chat, container, false);
        Log.e("cv", "cv");
        chatListViewAdapter = new ChatListViewAdapter(getActivity(), beans);
        init(view);
        chatListView.setAdapter(chatListViewAdapter);
        initEvent();
        return view;
    }

    private void init(View view) {
        chatListView = view.findViewById(R.id.chat_list);
        time = view.findViewById(R.id.chat_time);
    }

    private void initEvent() {
//        chatListView.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                PopupMenu popupMenu = new PopupMenu(getActivity(),v);
//                popupMenu.getMenuInflater().inflate(R.menu.fragment_list_poup,popupMenu.getMenu());
//                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
//                    @Override
//                    public boolean onMenuItemClick(MenuItem item) {
//                        switch (item.getItemId()){
//                            case R.id.item_delete:
//                                break;
//                            case R.id.item_toTop:
//                                break;
//                        }
//                        return false;
//                    }
//                });
//                return false;
//            }
//        });
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e("Start", "start");
        Cursor cursor = db.query("jipeng", null, null, null, null, null, "id desc","1");
//        if (cursor.getCount() > 0 && beans.size() == 0) {
//            while (cursor.moveToFirst()){
//                ChatBean bean = new ChatBean();
//                bean.setId();
//            }
//
//        }
//        if (bean == null){
//            bean = new ChatBean();
////            Cursor cursor = db.query("jipeng", new String[]{"token"}, null, null, null, null, "id desc", "1");
//            if (cursor.getCount() != 0) {
//                cursor.moveToLast();
//                bean.setId(1);
//                bean.setImage(R.mipmap.xjp);
//                bean.setTitle("辛集朋");
//                bean.setCurrent(cursor.getString(cursor.getColumnIndex("token")));
//                beans.add(bean);
////                chatListViewAdapter.notifyDataSetChanged();
//            }
//        }
//        if (cursor.getCount()>0 && beans.size()==0){
//
//        }
        if (cursor.getCount() > 0) {
            cursor.moveToLast();
            bean.setCurrent(cursor.getString(cursor.getColumnIndex("token")));
            bean.setImage(R.mipmap.xjp);
            bean.setTitle("辛集朋");
            bean.setTime(cursor.getString(cursor.getColumnIndex("time")));
            if (beans.size()==0) beans.add(bean);
        }

        chatListViewAdapter.notifyDataSetChanged();
    }
}
